# -*- coding: utf-8 -*-
from django.http import HttpResponse

from django.template.loader import get_template
from django.template import Context

from django.db import connection
from django.core import serializers
from django.core.serializers.json import DjangoJSONEncoder
from django.utils import simplejson

from db.models import Developer, Job
from django.core.context_processors import csrf
from django.http import QueryDict

def link_developer_with_parent(request):
    post = request.POST.dict();
    try:
        developer_id = int(post['developer_id'])
        parent_id = int(post['parent_id'])
        print "link_developer_with_parent", post, developer_id
        for key in post:
            print "kkey=", key;
        developer = Developer.objects.get(id=developer_id);
        parent = Developer.objects.get(id=parent_id);
        developer.parent = parent;
        developer.save();
    except Developer.DoesNotExist:
        print 'except Publisher.DoesNotExist:'
    data = simplejson.dumps([], cls=DjangoJSONEncoder)
    return HttpResponse(data, mimetype='application/json');
def link_job_with_parent(request):
    post = request.POST.dict();
    try:
        job_id = int(post['job_id'])
        parent_id = int(post['parent_id'])
        print "link_job_with_parent", post, job_id
        for key in post:
            print "kkey=", key;
        job = Job.objects.get(id=job_id);
        parent = Job.objects.get(id=parent_id);
        job.parent = parent;
        job.save();
    except Job.DoesNotExist:
        print 'except Publisher.DoesNotExist:'
    data = simplejson.dumps([], cls=DjangoJSONEncoder)
    return HttpResponse(data, mimetype='application/json');



def link_developer_with_job(request):
    post = request.POST.dict();
    try:
        developer_id = int(post['developer_id'])
        job_id = int(post['job_id'])
        print "link_developer_with_job", post, developer_id
        for key in post:
            print "kkey=", key;
        developer = Developer.objects.get(id=developer_id);
        job = Job.objects.get(id=job_id);
        job.developers.add(developer);
        #job.save();
    except Developer.DoesNotExist:
        print 'except Publisher.DoesNotExist:'
    data = simplejson.dumps([], cls=DjangoJSONEncoder)
    return HttpResponse(data, mimetype='application/json');
def hello(request, c):
    print c;
    ps='name'
    job = Job(name=ps);
    job.save()
    print c;
    return HttpResponse('');
 
    cursor = connection.cursor();
    t = get_template('hello.html');



    ulist = Developer.objects.all()
    print ulist

    html = t.render(Context({'users': ulist}));
    
    u = Developer(name='a1s2d3f4%s' % c);
    u.save();
    
    return HttpResponse(html);

def data_developers(request):
    def add_item(item):
        user = Developer(id=item['id'], name=item['name']);
        user.save()
    def get_items():
        users_list = Developer.objects.all()
        mlist = [];
        for user in users_list:
            jobs = [];
            for job in Job.objects.filter(developers=user):
                jobs.append({'id': job.id, 'name': job.name})
            try:
                parent = int(user.parent.id);
            except:
                parent = -1;
            mlist.append({'id': user.id, 'parent': parent, 'name': user.name, 'jobs': jobs});
        return mlist;
    return data(request, add_item, get_items);

def data_jobs(request):
    def add_item(item):
        job = Job.objects.get(id=item['id']);
        job.name = item['name'];
        job.save()
    def get_items():
        jobs_list = Job.objects.all()
        mlist = [];
        for job in jobs_list:
            developers = [];
            for developer in job.developers.all():
                developers.append({'id': developer.id, 'name': developer.name});
            try:
                parent = int(job.parent.id);
            except:
                parent = -1;
            mlist.append({'id': job.id, 'parent': parent, 'name': job.name, 'developers': developers});
        return mlist;
    return data(request, add_item, get_items);
def data(request, add_item, get_items):
    import types     
    def check_item(item):
        print "add item";
        if types.ListType == type(item):
            for i in item:
                check_item(i);
        else:
            print item
            add_item(item)
    for key in request.POST.dict():
        print "key=", key;
        item = simplejson.loads(key)
        check_item(item);
        data = simplejson.dumps([], cls=DjangoJSONEncoder)
        return HttpResponse(data, mimetype='application/json');
    data = simplejson.dumps(get_items(), cls=DjangoJSONEncoder)
    return HttpResponse(data, mimetype='application/json');
def load_index_file(request):
    t = get_template('index.html');
    html = t.render(Context());
    return HttpResponse(html);

def load_style_file(request, file_name):
    t = get_template(file_name + '.css');
    html = t.render(Context());
    return HttpResponse(html, mimetype='text/css');

def load_js_file(request, file_name):
    #t = get_template(file_name + '.js');
    #html = t.render(Context());
    html = get_file('planner/templates/' + file_name + '.js');
    print 'planner/templates/' + file_name + '.js';
    return HttpResponse(html, mimetype='application/x-javascript');
def load_image_gif_file(request, file_name):
    html = get_file('planner/templates/' + file_name + '.gif');
    return HttpResponse(html, mimetype='image/jpeg');
def load_prop_file(request, file_name):
    html = get_file('planner/templates/' + file_name + '.properties');
    return HttpResponse(html, mimetype='image/jpeg');
def load_image_svg_file(request, file_name):
    html = get_file('planner/templates/' + file_name + '.png');
    return HttpResponse(html, mimetype='image/jpeg');
def get_file(file_name):
    f = open(file_name, 'r');
    result = f.read();
    f.close();
    return result;
def load_graph(request):
    cursor = connection.cursor();
    t = get_template('mxgraph/examples/hovericons.html'); 
    html = t.render(Context());
    return HttpResponse(html);

